﻿Imports Oracle.DataAccess.Client
Public Class fBuscaTercero
    Private lIdTercero As String
    Private lSucursal As Integer
    Private lTipoTercero As String = "Tercero"
    Public ReadOnly Property midTercero() As String
        Get
            Return lIdTercero
        End Get
    End Property
    Public ReadOnly Property mSucursal() As Integer
        Get
            Return lSucursal
        End Get
    End Property
    Public Property TipoTercero() As String
        Set(ByVal value As String)
            lTipoTercero = value
        End Set
        Get
            Return lTipoTercero
        End Get
    End Property

    Private Sub buscaTercero()
        Try
            Dim conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Dim cmd As OracleCommand = conn.CreateCommand
            Dim qFrom As String = "FROM TER_TERCERO "
            Dim qSelect As String = "SELECT TER_TERCERO.IDTERCERO, TER_TERCERO.NOMBRES, TER_TERCERO.APELLIDOS, TER_TERCERO.DIRECCION, TER_TERCERO.DOCIDENT, TER_TERCERO.ESTADO, TER_TERCERO.IDMUNICIPIO, TER_TERCERO.RAZONSOCIAL, TER_TERCERO.TELEFONO, TER_TERCERO.TIPODOCIDEN "
            Dim qWhere As String = "WHERE "
            TER_TERCERODataGridView.Columns("SUCURSAL").Visible = False
            Select Case lstTipoTercero.Text
                Case "Cliente"
                    qFrom &= "INNER JOIN TER_CLIENTES ON TER_TERCERO.IDTERCERO=TER_CLIENTES.IDCLIENTE "
                    qSelect &= ", TER_CLIENTES.SUCURSAL "
                    TER_TERCERODataGridView.Columns("SUCURSAL").Visible = True
                Case "Proveedor"
                    qFrom &= "INNER JOIN TER_PROVEEDORES ON TER_TERCERO.IDTERCERO=TER_PROVEEDORES.IDPROVEEDOR "
                Case "Vendedor"
                    qFrom &= "INNER JOIN TER_VENDEDOR ON TER_TERCERO.IDTERCERO=TER_VENDEDOR.IDVENDEDOR "
            End Select
            If txtNomRazon.Text.Trim.Length > 0 Then
                If IsNumeric(txtNomRazon.Text) Then
                    qWhere &= "IDTERCERO=:PIDTERCERO AND (TER_TERCERO.ESTADO = '1')"
                    cmd.Parameters.Add("PIDTERCERO", OracleDbType.Varchar2).Value = txtNomRazon.Text
                Else
                    If rbNombre.Checked Then _
                    qWhere &= "(UPPER(TER_TERCERO.NOMBRES || TER_TERCERO.APELLIDOS) LIKE :PNOMAPE) AND (TER_TERCERO.ESTADO = '1')"
                    If rbRazon.Checked Then _
                        qWhere &= "(UPPER(TER_TERCERO.RAZONSOCIAL) LIKE :PNOMAPE) AND (TER_TERCERO.ESTADO = '1')"
                    cmd.Parameters.Add("PNOMAPE", OracleDbType.Varchar2).Value = "%" & Me.txtNomRazon.Text.ToUpper & "%"
                End If
            End If
            If qWhere = "WHERE " Then _
                qWhere = ""
            cmd.CommandText = qSelect + qFrom + qWhere
            Dim DtTer As New DataTable
            conn.Open()
            DtTer.Load(cmd.ExecuteReader)
            conn.Close()
            TER_TERCERODataGridView.DataSource = DtTer
            TER_TERCERODataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try

    End Sub

    Private Sub btnBusca_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBusca.Click
        Try
            buscaTercero()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub fBuscaTercero_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        lstTipoTercero.Text = lTipoTercero
        If lTipoTercero <> "Tercero" Then
            Me.Text &= "-" & lTipoTercero
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub AsignaTercero(ByVal Fila As Integer)
        If Fila >= 0 Then
            lIdTercero = TER_TERCERODataGridView.Rows(Fila).Cells("IDTERCERO").Value
            If lTipoTercero = "Cliente" Then
                lSucursal = TER_TERCERODataGridView.Rows(Fila).Cells("SUCURSAL").Value
            End If
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub
    Private Sub TER_TERCERODataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles TER_TERCERODataGridView.CellMouseDoubleClick
        AsignaTercero(e.RowIndex)
    End Sub

    Private Sub TER_TERCERODataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TER_TERCERODataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaTercero(TER_TERCERODataGridView.CurrentRow.Index)
        End If
    End Sub

    Private Sub txtNomRazon_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNomRazon.KeyDown
        Try

            Select Case e.KeyCode
                Case 13
                    buscaTercero()
                Case Else

            End Select

        Catch ex As Exception
            Throw ex
        End Try
    End Sub


    'Private Sub txtNomRazon_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNomRazon.Enter
    '    Try

    '        buscaTercero()
    '    Catch ex As Exception
    '        Throw
    '    End Try
    'End Sub

    'Private Sub txtNomRazon_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNomRazon.KeyPress
    '    Try
    '        Dim caracter As String = 
    '        If caracter = " " Then
    '            buscaTercero()
    '        End If
    '    Catch ex As Exception
    '        Throw ex
    '    End Try
    'End Sub
End Class